package com.ruoyi.gateway.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

// @Order(-1)

@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 1.获取请求参数
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getURI().getPath();
        System.out.println("请求=="+path);

        //api接口，异步请求，校验用户必须登录
//        if(antPathMatcher.match("/**/auth/**",path)) {
//            ServerHttpResponse response = exchange.getResponse();
//            if(StringUtils.isEmpty(token)) {
//                //不存在token 未登录
//                log.error("token not found-未登录");
//
//                return out(response, ResultCodeEnum.Login4_ERROR);
//            }
//            //存在token,校验
//            boolean isVerify = JwtHelper.verifyToken(token);
//            if(!isVerify){
//                //token不合法
//                log.error("invalid token-token不合法");
//                return out(response, ResultCodeEnum.Login5_ERROR);
//            }
//            //token 合法且正确
//            //更新token
//            //TODO
//        }

        return chain.filter(exchange);

    }




    @Override
    public int getOrder() {
        return -1;
    }
}
